package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.List;

@RestController
@RequestMapping("permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     * 分页查询权限列表
     *
     * @param queryPageBean
     * @return
     */
    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean,Principal principal) {
        String name = principal.getName();
        try {
            PageResult pageResult = permissionService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString(),name);
            return new Result(true, MessageConstant.QUERY_PURVIEWLIST_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PURVIEWLIST_FAIL);
        }
    }

    /**
     * 查询所有权限用于添加列表
     *
     * @return
     */
    @RequestMapping("findAllPermission")
    public Result findAllPermission() {
        try {
            List<Permission> permissionList = permissionService.findAllPermission();
            return new Result(true, MessageConstant.QUERY_PURVIEWLIST_SUCCESS, permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PURVIEWLIST_FAIL);
        }

    }

    /**
     * 通过用户名查询权限id
     *
     * @param principal
     * @return
     */
    @RequestMapping("findPermissionIdsByRoleName")
    public Result findPermissionIdsByRoleName(Principal principal) {
        String name = principal.getName();
        try {
            List<Integer> permissionIds = permissionService.findPermissionIdsByRoleName(name);
            return new Result(true, MessageConstant.QUERY_PURVIEWLIST_SUCCESS, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PURVIEWLIST_FAIL);
        }
    }

    /**
     * 添加/修改权限
     *
     * @param permissionIds
     * @param principal
     * @return
     */
    @RequestMapping("edit")
    public Result edit(@RequestParam("permissionIds") Integer[] permissionIds, Principal principal) {
        String name = principal.getName();
        try {
            permissionService.edit(name, permissionIds);
            return new Result(true, MessageConstant.EDIT_PURVIEWLIST_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PURVIEWLIST_FAIL);
        }
    }

    /**
     * 根据id删除权限
     * @param id 权限id
     * @param principal
     * @return
     */
    @RequestMapping("deleteById")
    public Result deleteById(@RequestParam("id") Integer id, Principal principal) {
        String name = principal.getName();
        try {
            permissionService.deleteById(id, name);
            return new Result(true, MessageConstant.DELETE_PURVIEWLIST_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PURVIEWLIST_FAIL);
        }

    }
}
